package com.threeonemeeting.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.threeonemeeting.R;
import com.threeonemeeting.model.EnrollUserInfo;

public class EnrollUserInfoListAdapter extends ArrayAdapter<EnrollUserInfo> {

	private ArrayList<EnrollUserInfo> mItemsList;

	public EnrollUserInfoListAdapter(Activity activity) {
		super(activity, 0);
	}

	public void setUserList(ArrayList<EnrollUserInfo> userList) {
		mItemsList = userList;
		notifyDataSetChanged();
	}

	public ArrayList<EnrollUserInfo> getUserList() {
		return mItemsList;
	}

	@Override
	public void add(EnrollUserInfo userInfo) {
		if (mItemsList == null)
			mItemsList = new ArrayList<EnrollUserInfo>();

		mItemsList.add(userInfo);
	}

	public void clear() {
		if (mItemsList != null) {
			mItemsList.clear();
		}
	}

	@Override
	public int getCount() {
		if (mItemsList == null)
			return 0;

		return mItemsList.size();
	}

	@Override
	public EnrollUserInfo getItem(int position) {
		return mItemsList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		Activity activity = (Activity) getContext();

		EnrollUserInfo userInfo = getItem(position);

		View rowView = convertView;
		EnrollUserInfoCache viewCache;

		if (rowView == null) {
			LayoutInflater inflater = activity.getLayoutInflater();
			rowView = inflater.inflate(R.layout.lvi_enroll_user, null);
			viewCache = new EnrollUserInfoCache(rowView, 0,
					userInfo.strRealName, userInfo.strUserMobileNo);
			rowView.setTag(viewCache);
		} else {
			viewCache = (EnrollUserInfoCache) rowView.getTag();
		}

		// 报名用户名
		TextView userNameView = viewCache.getUserNameTextView();
		userNameView.setText(viewCache.getUserName());

		// 报名用户手机号
		TextView userMobileNoView = viewCache.getUserMobileNoTextView();
		userMobileNoView.setText(viewCache.getUserMobileNo());

		return rowView;
	}

	public class EnrollUserInfoCache {

		private View baseView;
		private int nMeetingId;
		private String strUserName;
		private String strMobileNo;

		private TextView userNameTextView;
		private TextView userMobileNoTextView;

		public EnrollUserInfoCache(View baseView, int nMeetingId,
				String strUserName, String strUserMobileNo) {
			this.baseView = baseView;
			this.nMeetingId = nMeetingId;
			this.strUserName = strUserName;
			this.strMobileNo = strUserMobileNo;
		}

		public int getMeetingId() {
			return this.nMeetingId;
		}

		public String getUserName() {
			return this.strUserName;
		}

		public String getUserMobileNo() {
			return this.strMobileNo;
		}

		public TextView getUserNameTextView() {
			if (userNameTextView == null) {
				userNameTextView = (TextView) baseView
						.findViewById(R.id.user_name);
			}
			return userNameTextView;
		}

		public TextView getUserMobileNoTextView() {
			if (userMobileNoTextView == null) {
				userMobileNoTextView = (TextView) baseView
						.findViewById(R.id.user_mobile_no);
			}
			return userMobileNoTextView;
		}
	}
}
